package logik;

import gui.Screen;
import gui.UserMenu;
import gui.Welcome;
import data.BankDatabase;
import data.User;
import java.awt.event.*;

import javax.swing.JOptionPane;

/**
 * @author Besitzer
 * ATM.java
 * Represents an automated teller machine
 */
public class ATM {

	private boolean userAuthenticated; // whether user is authenticated
	private int currentAccountNumber; // current user's account number
	private Screen screen; // ATM's screen
	private CashDispenser cashDispenser; // ATM's cash dispenser
	private DepositSlot depositSlot; // ATM's deposit slot
	private BankDatabase bankDatabase; // account information database
	private String accountType = null;
	private User user;
	private Admin admin;
	private UserMenu usermenu;
	private Welcome welcome;
	private int maxAcc;
	private int pin;
	private AuthenticateUser authenticater;
	private User users;

	/**
	 * no-argument ATM constructor initializes instance variables
	 */
	public ATM() {
		userAuthenticated = false; // user is not authenticated to start
		currentAccountNumber = 0; // no current account number to start
		screen = new Screen(); // create screen
		cashDispenser = new CashDispenser(); // create cash dispenser
		bankDatabase = new BankDatabase(); // create acct info database
		admin = new Admin(bankDatabase, screen, user); // create admit
		usermenu = new UserMenu(currentAccountNumber, userAuthenticated, bankDatabase, 
				screen, accountType, this, maxAcc);
		authenticater = new AuthenticateUser(screen, bankDatabase);

		// Listener for all GUIs
		screen.getWelcome().addActionListener(new WelcomeActionListener());
		screen.getAdminMenu().addActionListener(new AdminMenuActionListener());
		screen.getUserMenu().addActionListener(new UserMenuActionListener());

	} // end no-argument ATM constructor

	/**
	 * start ATM
	 */
	public final void run() {
		// welcome and authenticate user; perform transactions
		// loop while, user is not yet authenticated
		if (!userAuthenticated) {
			screen.show(Screen.WELCOME);
			screen.setVisible(true);
		} // end while
		// Checks if current user isn't admin
		//			System.out.println();
		//			currentAccountNumber = authenticater.getAccNr();
		//			System.out.println(currentAccountNumber);
		//			if (!bankDatabase.getUsers(currentAccountNumber).isAdmin) {
		//				System.out.println("PASSED"); 
		//			}					   // and continues to user menu
		//			usermenu.userAuthenticated = false; // reset before next ATM session
		//			currentAccountNumber = 0; // reset before next ATM session 	  
	} // end method run

	// Listeners for all components within the GUIs
	// Welcome listener
	private class WelcomeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event){
			
				authenticater.authenticateUser();
				if(authenticater.getUsAu())
					screen.show(Screen.ADMINMENU);
				else{ // skal laves som metode, ligesom keypad, dvs. gemme exception handlers seperate og hente dem
					JOptionPane.showMessageDialog(null, "Wrong Input", 
							"and lala", JOptionPane.WARNING_MESSAGE);
					screen.show(Screen.WELCOME);

				}
		}
	}

	// AdminMenu listener
	private class AdminMenuActionListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			screen.show(Screen.ADMINMENU);
		}
	}

	//UserMenu listener
	private class UserMenuActionListener implements ActionListener{
			
			
		}
	}



}
